 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Library
{
    public class Regenerator
    {
        public List<Client> ClientRegenerator(string nameOrEgn)
        {
            List<Client> findedClients = new List<Client>();

            using (StreamReader reader = new StreamReader(@"clients.txt", Encoding.GetEncoding(1251)))
            {
                string line = reader.ReadLine();

                while (line != null)
                {
                    string[] fields = line.Split('|');

                    foreach (var field in fields)
                    {
                        if (field == nameOrEgn)
                        {
                            ulong egn = ulong.Parse(fields[2]);
                            Client regenerated = new Client(fields[0], fields[1], egn, fields[3]);
                            InitalizeTempData(regenerated);
                            findedClients.Add(regenerated);
                        }
                    }
                    line = reader.ReadLine();
                }
            }
            return findedClients;
        }

        private void InitalizeTempData(Client reg)
        {
            using (StreamReader reader = new StreamReader(@"tenantDataTaken.txt", Encoding.GetEncoding(1251)))
            {
                string line = reader.ReadLine();

                while (line != null)
                {
                    string[] fields = line.Split('|');

                    foreach (var field in fields)
                    {
                        if (field == reg.EGN.ToString())
                        {
                            reg.Taken.Add(BookRegenerator(fields[3])[0]);
                        }
                    }
                    line = reader.ReadLine();
                }
            }
        }

        public List<Book> BookRegenerator(string titleOrAuthorOrIDNumber)
        {
            List<Book> findedBooks = new List<Book>();

            using (StreamReader reader = new StreamReader(@"books.txt", Encoding.GetEncoding(1251)))
            {
                string line = reader.ReadLine();

                while (line != null)
                {
                    string[] fields = line.Split('|');

                    foreach (var field in fields)
                    {
                        if (field == titleOrAuthorOrIDNumber)
                        {
                            bool eCopy = StringToBool(fields[2]);
                            DepartmentsName departmentLocation = StringToDepartmentsName(fields[4]);
                            Genre genre = StringToGenre(fields[5]);

                            Book regenerated = new Book(fields[0], fields[1], eCopy, fields[3], departmentLocation, genre);
                            InitalizeTempData(regenerated);
                            findedBooks.Add(regenerated);
                        }
                    }
                    line = reader.ReadLine();
                }
            }
            return findedBooks;
        }

        public Genre StringToGenre(string genreField)
        {
            Genre genre = Genre.Fiction;

            switch (genreField)
            {
                case "Fiction":
                    genre = Genre.Fiction;
                    break;
                case "Crime":
                    genre = Genre.Crime;
                    break;
                case "Romance":
                    genre = Genre.Romance;
                    break;
                case "Lyrics":
                    genre = Genre.Lyrics;
                    break;
                case "Poetry":
                    genre = Genre.Poetry;
                    break;
                case "Science":
                    genre = Genre.Science;
                    break;
                case "Thriller":
                    genre = Genre.Thriller;
                    break;
                case "Esotericism":
                    genre = Genre.Esotericism;
                    break;
                case "Fantasy":
                    genre = Genre.Fantasy;
                    break;
                default:
                    break;
            }
            return genre;
        }

        public DepartmentsName StringToDepartmentsName(string departmentLocationField)
        {
            DepartmentsName departmentLocation = DepartmentsName.Storage;

            switch (departmentLocationField)
            {
                case "Storage":
                    departmentLocation = DepartmentsName.Storage;
                    break;
                case "Registration":
                    departmentLocation = DepartmentsName.Registration;
                    break;
                case "ReadingRoom":
                    departmentLocation = DepartmentsName.ReadingRoom;
                    break;
                case "BorrowRoom":
                    departmentLocation = DepartmentsName.BorrowRoom;
                    break;
                case "KidsRoom":
                    departmentLocation = DepartmentsName.KidsRoom;
                    break;
                default:
                    break;
            }
            return departmentLocation;
        }

        public bool StringToBool(string boolField)
        {
            if (boolField.ToLower() == "true")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void InitalizeTempData(Book reg)
        {
            using (StreamReader reader = new StreamReader(@"tenantDataTaken.txt", Encoding.GetEncoding(1251)))
            {
                string line = reader.ReadLine();

                while (line != null)
                {
                    string[] fields = line.Split('|');

                    foreach (var field in fields)
                    {
                        if (field == reg.IDNumber)
                        {
                            reg.ClientBorrowIt = ulong.Parse(fields[0]);
                            reg.available = false;
                            reg.EndBorrowPeriod = DateTime.Parse(fields[2]);
                            return;
                        }
                    }
                    line = reader.ReadLine();
                }
                reg.ClientBorrowIt = default(long);
                reg.available = true;
                reg.EndBorrowPeriod = default(DateTime);
            }
        }
    }
}
